import axios from 'axios'
import store from '@/store'
import bigint from 'json-bigint'
// 封装带有默认地址的axios对象
const request = axios.create({
  baseURL: 'http://ttapi.research.itcast.cn/',
  transformResponse: [function(data) {
    try {
      return bigint.parse(data)
    } catch (error) {
      return data
    }
  }]
})
// 设置请求拦截器，给请求头添加token
request.interceptors.request.use(
  function (config) {
    const user = store.state.user
    if (user != null) {
      config.headers.Authorization = `Bearer ${store.state.user.token}`
    }
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

export default request